<template>
  <div class="page-container">
    <el-card class="password-card">
      <template #header>
        <h2>重置密码</h2>
      </template>
      
      <el-form :model="form" label-position="top">
        <el-form-item label="新密码" prop="newPassword">
          <el-input 
            v-model="newPassword" 
            type="password" 
            placeholder="请输入新密码"
            show-password
          />
        </el-form-item>
        
        <el-form-item label="确认新密码" prop="confirmPassword">
          <el-input 
            v-model="confirmPassword" 
            type="password" 
            placeholder="请再次输入新密码"
            show-password
          />
        </el-form-item>
        
        <el-button 
          type="primary" 
          @click="resetPassword" 
          class="submit-btn"
          :loading="loading"
        >
          确认修改
        </el-button>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import axios from 'axios';
// 获取 Cookie 的工具函数
function getCookie(name) {
  const value = `; ${document.cookie}`;
  const parts = value.split(`; ${name}=`);
  if (parts.length === 2) return parts.pop().split(';').shift();
}

export default {
  data() {
    return {
      newPassword: '',
      confirmPassword: '',
      loading: false
    };
  },
  
  methods: {
    async resetPassword() {
      if (!this.newPassword || !this.confirmPassword) {
        this.$message.error('请填写所有字段');
        return;
      }
      if (this.newPassword !== this.confirmPassword) {
        this.$message.error('两次输入的密码不一致');
        return;
      }
      this.loading = true;
      try {
        // 尝试从cookie获取手机号或邮箱
        const phone = getCookie('reset_phone');
        const email = getCookie('reset_email');
        
        const response = await axios.post('/api/users/reset-password/', {
          phone: phone,
          email: email,
          password: this.newPassword
        },{
          headers:{
            'Authorization': `Bearer ${getCookie('reset_token')}`
          }
        });
        if (response.data.status === 'success') {
          this.$message.success('密码重置成功');
          // 清除所有重置相关的cookie
          document.cookie = 'reset_token=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;';
          document.cookie = 'reset_phone=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;';
          document.cookie = 'reset_email=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;';
          this.$router.push('/login');
        } else {
          this.$message.error(response.data.message || '密码重置失败');
        }
      } catch (error) {
        if (error.response?.status === 401) {
          this.$message.error('Token 已过期，请重新申请重置');
          this.$router.push('/forgot-password');
        } else {
          this.$message.error(error.response?.data?.message || '网络错误，请重试');
        }
      } finally {
        this.loading = false;
      }
    }
  }
};
</script>
<style scoped>
/* 原有样式保持不变 */
.page-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
}

.password-card {
  width: 400px;
  text-align: center;
}

h2 {
  margin: 0;
  font-size: 1.5rem;
}

.submit-btn {
  width: 100%;
  margin-top: 20px;
}

.el-form-item {
  margin-bottom: 20px;
}
</style>